// JS enhancements
if (typeof Object.create !== 'function') {
	Object.create = function(o) {
		var F = function() {};
		F.prototype = o;
		return new F();
	}
};

Function.prototype.method = function (name, func) {
	if (!this.prototype[name]) {
		this.prototype[name] = func;
	}
	return this;
};

Function.method('inherits', function(Parent) {
	this.prototype = new Parent();
	return this;
});

Object.method('superior', function(name) {
	var that = this, 
		method = that[name];
		return function() {
			return method.apply(that,arguments);
		};
});


Number.method('integer', function() {
	return Math[this < 0 ? 'ceiling' : 'floor'](this);
});

String.method('trim', function() {
	return this.replace(/^\s+|\s+$/g, '');
});


//util functions
function setCookie(name,value,daysTilExpire) {
	daysTilExpire = daysTilExpire || null;
	
	var expires=new Date();
	if (daysTilExpire!=null) expires.setDate(expires.getDate() + daysTilExpire);
	
	document.cookie = name + "="  +escape(value) + ((daysTilExpire==null) ? "" : ";expires=" + expires.toUTCString());
};

function getCookie(name) {
	if (document.cookie.length>0) {
  		val_start=document.cookie.indexOf(name + "=");
  		if (val_start!=-1) {
    		val_start=val_start + name.length+1;
    		val_end=document.cookie.indexOf(";",val_start);
    		if (val_end==-1) val_end=document.cookie.length;
   			 return unescape(document.cookie.substring(val_start,val_end));
    	}
  	}
	return null;
};

function createUUID() {
	var uuid = [];
	var chars = String("0123456789ABCDEF").split("");
	var reserved = String("89ab").split("");
	
	for (var i = 0; i <36; i++) uuid[i] = chars[Math.floor(Math.random()*15)];
	uuid[14] = 4;
	uuid[19] = reserved[Math.floor(Math.random()*3)];
	
	uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
	return uuid.join('');
};

/*function service(spec) {
	var that = {};
	
	that.call = function(request) {
		$.ajax({
			url:spec.url, global:false, type:"POST", data:request, dataType:"json", success:function(msg){ spec.callback(msg); }
		});
	};
	
	return that;
};*/

var Service = function() {};

Service.prototype.call = function(uri,request,callback) {
    $.ajax({
        url: uri, global: false, type: "POST", data: request, dataType: "json", success: function(msg) { callback(msg); }
    });
}

function randomizeArray(originalArray,returnLength) {
	if (originalArray==null) return [];
	returnLength = returnLength || 0;
	
	var randomArray = [];
	var orderedArray = originalArray.concat();
	if (returnLength==0) returnLength = originalArray.length;
			
	for (var n=0;n<returnLength;n++) {
		var tmpNum = getRandomNum(orderedArray.length-1);
				
		randomArray[n] = orderedArray[tmpNum];
		orderedArray.splice(tmpNum, 1);
	}
			
	return randomArray;
}
        
function getRandomNum(max, min) {
	if (max==null) return -1;
	min = min || 0;
			
	//return a number between 0 and max
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function sortByProp(prop) {
	var that = {};
	that.prop = prop;
	that.execute = function(a,b) {
		var x = a[this.prop];
		var y = b[this.prop];
		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	}
	return that;
}

function isGearsAvailable() {
	if (window.google && google.gears) return true;
	else return false;
}

var window_stack = new Array();
function createPopupWindow (url,name,props) {
	var newWin = window.open(url,name,props);
	window_stack[name] = newWin;
	newWin.focus();
}
	
function closePopup(name) {
	if (window_stack[name]) window_stack[name].close();
}

function createBookmark(title,url,isAlert,alertCopy) {
	if (ie && window.external) {
		window.external.AddFavorite(url, title);
		return true;
	} else {
		str=(mac?'Command/Cmd':'CTRL');
		str+=' + D';
		
		if (isAlert) {
			alert((alertCopy!=""?alertCopy:'After closing this, ')+str);
			return true;
		}
		else return str;
	}
}